package com.keyinong.caizhengting.myfragement;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.Utility;
import com.keyinong.caizhengting.adapter.MycollectAdapter;
import com.keyinong.caizhengting.entity.MycollectEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 我的收藏
 */

public class MycollectActivity extends Activity {
    /**
     * 获得数据
     */
    ArrayList<MycollectEntity> infolist;
    HttpTools httptools = null;
    String token = "";
    int offset = 0;
    int page = 10;
    private MycollectAdapter mycollectAdapter;
    /**
     * 商品列表
     */
    private ListView lv;
    /**
     * 返回
     */
    private ImageButton ib_fanhui;
    /**
     * 是否有收藏
     */
    private TextView tv_more;
    private String spid;
    private String spname;
    private String spprice;
    private String spamount;
    private String arraylength;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_mycollect);
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        lv = (ListView) findViewById(R.id.lv_mycollect);
        tv_more = (TextView) findViewById(R.id.tv_more);
        Mycollect();
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui5);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }

    private void Mycollect() {
        infolist = new ArrayList<MycollectEntity>();
        Log.i("shi", "token---" + token);
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.mycollect(token, "" + page + "", offset + "", new JsonMycollect());
    }

    public class JsonMycollect extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONArray response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
//				Log.i("info",response.toString());
                for (int i = 0; i < response.length(); i++) {
                    JSONObject data = response.getJSONObject(i);
                    String id = data.getString("id");
                    String name = data.getString("name");
                    String content = data.getString("content");
                    String price = data.getString("price");
                    String image = data.getString("image");
                    String amount = data.getString("amount");
                    JSONArray array = data.getJSONArray("specs");
                    arraylength = "" + array.length();
                    if (array.length() != 0) {
                        for (int j = 0; j < array.length(); j++) {
                            JSONObject data2 = array.getJSONObject(j);
                            spid = data2.getString("id");
                            spname = data2.getString("name");
                            spprice = data2.getString("price");
                            spamount = data2.getString("amount");
                        }
                    }
                    infolist.add(new MycollectEntity(id, name, image, content, price, amount, spid, spname, spprice, spamount, arraylength));
                }
                int length = response.length();
                if (length == 0) {
                    tv_more.setVisibility(View.VISIBLE);
                } else {
                    tv_more.setVisibility(View.GONE);
                }
                mycollectAdapter = new MycollectAdapter(MycollectActivity.this, infolist);
                lv.setAdapter(mycollectAdapter);
                lv.setDivider(null);
                Utility.setListViewHeightBasedOnChildren(lv);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}
